package common.util;

import cn.hutool.core.util.StrUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 工具类操作String类型的公共方法类
 */
public class StringUtil {

    private final static String regxpForHtml = "</([^>]*)>"; // 过滤所有以<开头以>结尾的标签
    private final static String regxpForHtml2 = "<([^>]*)>"; // 过滤所有以<开头以>结尾的标签

    /**
     * 判断字符串是否为空或null
     */
    public static boolean isEmpty(CharSequence cs) {
        return StrUtil.isEmpty(cs);
    }

    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.size() == 0;
    }

    /**
     * 判断字符串是否为空
     * todo gcr
     */
    public static boolean isNotNull(String str) {
        if (str != null && !str.trim().equals("") && !str.equals("null")) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 判断对象是否为空
     * todo gcr
     */
    public static boolean isNotNull(Object str) {
        if (str != null && !str.equals("") && !str.equals("null") && !str.toString().trim().equals("")) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 判断对象是否为空
     * todo gcr
     */
    public static boolean isNull(Object str) {
        if (str == null || str.equals("") || str.equals("null") || str.toString().trim().equals("")) {
            return true;
        } else {
            return false;
        }
    }

    public static String filterHtml(String str) {
        Pattern pattern = Pattern.compile(regxpForHtml);
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        boolean result1 = matcher.find();
        while (result1) {
            matcher.appendReplacement(sb, "");
            result1 = matcher.find();
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String filterHtml2(String str) {
        Pattern pattern = Pattern.compile(regxpForHtml2);
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        boolean result1 = matcher.find();
        while (result1) {
            matcher.appendReplacement(sb, "/");
            result1 = matcher.find();
        }
        matcher.appendTail(sb);
        return sb.toString();
    }


    /**
     * encodeUrl
     */
    public static String encodeUrl(String url) {
        try {
            url = URLEncoder.encode(url, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return url;
    }

    /**
     * 判断字符有没有中文 暂且这样
     *
     * @param str
     * @return
     */
    public static Boolean isContainsChina(String str) {
        if (null == str) {
            return false;
        }
        str = str.toLowerCase().trim();

        String address = str;
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(address);
        if (m.find()) {
            // m.find()判断为true，表示字符串中含有中文
            return true;
        } else {
            return false;
        }

//        List<String> list = Arrays.asList("anhui ", "beijing ", "changchun ", "changsha ", "chengdu ", "china ", "chongqing ", "fujian ", "fuzhou ", "gansu ", "guangdong ", "guangxi ", "guangzhou ", "guiyang ", "guizhou ", "haikou ", "hainan ", "hangzhou ", "harbin ", "hebei ", "hefei ", "heilongjiang ", "henan ", "hohhot ", "hong kong ", "hubei ", "hunan ", "inner mongolia ", "jiangsu ", "jiangxi ", "jilin ", "jinan ", "kunming ", "lanzhou ", "lhasa ", "liaoning ", "macao ", "nanchang ", "nanjing ", "nanning ", "ningxia ", "qinghai ", "shaanxi ", "shandong ", "shanghai ", "shanxi ", "shenyang ", "shijiazhuang ", "sichuan ", "taipei ", "taiwan ", "taiyuan ", "tianjin ", "tibet ", "urumqi ", "wuhan ", "xi'an ", "xining ", "xinjiang ", "yinchuan ", "yunnan ", "zhejiang ", "zhengzhou ", "中国", "中华人民共和国", "北京", "浙江", "天津", "安徽", "上海", "福建", "重庆", "江西", "香港", "山东", "澳门", "河南", "内蒙古", "湖北", "新疆", "湖南", "宁夏", "广东", "西藏", "海南", "广西", "四川", "河北", "贵州", "山西", "云南", "辽宁", "陕西", "吉林", "甘肃", "黑龙江", "青海", "江苏", "台湾", "杭州", "合肥", "福州", "南昌", "济南", "郑州", "呼和浩特", "武汉", "乌鲁木齐", "长沙", "银川", "广州", "拉萨", "海口", "南宁", "成都", "石家庄", "贵阳", "太原", "昆明", "沈阳", "西安", "长春", "兰州", "哈尔滨", "西宁", "南京", "台北", "解放军", "重大专项", "温州", "深圳");
//        for (String s : list) {
//            if (str.contains(s)) {
//                return true;
//            }
//        }

    }


}
